<template>
  <div class="container">
    <div class="wrapper">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span style="font-size: 30px; color: cornflowerblue">个人中心</span>
        </div>
        <el-tabs v-model="activeTab">
          <el-tab-pane label="基本资料" name="userinfo">
            <el-form ref="form" :model="user" :rules="rules" label-width="80px">
              <el-form-item label="用户昵称" prop="nickName">
                <el-input v-model="user.nickName" maxlength="30" />
              </el-form-item>
              <el-form-item label="手机号码" prop="phonenumber">
                <el-input v-model="user.phonenumber" maxlength="11" />
              </el-form-item>
              <el-form-item label="性别">
                <el-radio-group v-model="user.sex">
                  <el-radio label="0">男</el-radio>
                  <el-radio label="1">女</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" size="mini" @click="submit"
                  >保存</el-button
                >
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="修改密码" name="resetPwd">
            <el-form
              ref="pwdForm"
              :model="pwdUser"
              :rules="pwdRules"
              label-width="80px"
            >
              <el-form-item label="旧密码" prop="oldPassword">
                <el-input
                  v-model="pwdUser.oldPassword"
                  placeholder="请输入旧密码"
                  type="password"
                  show-password
                />
              </el-form-item>
              <el-form-item label="新密码" prop="newPassword">
                <el-input
                  v-model="pwdUser.newPassword"
                  placeholder="请输入新密码"
                  type="password"
                  show-password
                />
              </el-form-item>
              <el-form-item label="确认密码" prop="confirmPassword">
                <el-input
                  v-model="pwdUser.confirmPassword"
                  placeholder="请确认密码"
                  type="password"
                  show-password
                />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" size="mini" @click="submit2"
                  >保存</el-button
                >
              </el-form-item>
            </el-form>
          </el-tab-pane>
          <el-tab-pane label="我的订单" name="hotelInfo">
            <el-table :data="tableData" style="width: 100%" stripe>
              <el-table-column prop="roomName" label="自习室房间名" width="240">
              </el-table-column>
              <el-table-column prop="seatName" label="座位" width="120">
              </el-table-column>
              <el-table-column prop="price" label="价格" width="80">
              </el-table-column>
              <el-table-column prop="status" label="状态"></el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button
                          size="mini"
                          type="primary"
                          @click="handleCancel(scope.row)"
                          v-show="scope.row.status==='预定中'"
                  >取消预定
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane label="成为会员" name="memberInfo">
            <el-form  label-width="80px">
              <el-form-item label="会员套餐" prop="name">
                <el-input  disabled="" value="终身会员99.99"></el-input>
              </el-form-item>
              <el-form-item label="会员特权" prop="content">
                <el-input  disabled="" value="1、会员折扣。
                  2、可享受vip座位。"></el-input>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" size="mini" @click="submit3()"
                >开通会员
                </el-button
                >
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import {
  getUserProfile,
  updateUserProfile,
  updateUserPwd,
  removeToken,
    orderInfo,addViper,updateItem
} from "@/utils/request";
export default {
  name: "Person",
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.pwdUser.newPassword !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      tableData: [],
      user: {
        nickName: "",
        phonenumber: "",
        sex: "",
      },
      activeTab: "userinfo",
      // 表单校验
      rules: {
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" },
        ],
        phonenumber: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur",
          },
        ],
      },
      pwdUser: {
        oldPassword: "",
        newPassword: "",
        confirmPassword: "",
      },
      // 表单校验
      pwdRules: {
        oldPassword: [
          { required: true, message: "旧密码不能为空", trigger: "blur" },
        ],
        newPassword: [
          { required: true, message: "新密码不能为空", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个字符",
            trigger: "blur",
          },
        ],
        confirmPassword: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { required: true, validator: equalToPassword, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    getUser() {
      getUserProfile().then((res) => {
        if (res.code === 200) {
          this.user.nickName = res.data.nickName;
          this.user.phonenumber = res.data.phonenumber;
          this.user.sex = res.data.sex;
        }
      });
    },
      handleCancel(row) {
          const datas ={
              seatId:row.seatId,
              id:row.id
          }
          updateItem(datas).then(() => {
              this.$message({
                  message: "取消成功",
                  type: "success",
              });
          }).catch(() => {
          });
      },
    submit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updateUserProfile(this.user).then((response) => {
            this.$message({
              message: "修改成功",
              type: "success",
            });
          });
        }
      });
    },
    submit2() {
      this.$refs["pwdForm"].validate((valid) => {
        if (valid) {
          updateUserPwd(
            this.pwdUser.oldPassword,
            this.pwdUser.newPassword
          ).then((response) => {
            this.$message({
              message: "修改成功",
              type: "success",
            });
          });
        }
      });
    },
      submit3(){
          const data={}
          addViper(data).then(res =>{
              this.$message({
                  message: "开通成功",
                  type: "success",
              });
          })
      },
      getOrderInfo() {
          orderInfo().then((res) => {
              if (res.code === 200) {
                  this.tableData = res.data;
              }
          });
      },
  },
  created() {
    this.getUser();
    this.getOrderInfo()
  },
};
</script>

<style scoped>
.text {
  font-size: 14px;
}
.container {
  height: 500px;
  width: 100%;
  background-color: #f4f4f4;
  padding: 1em 0;
}
.wrapper {
  padding: 40px 0px 0px 200px;
}
.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 1100px;
}
</style>
